import { reqGoodsDetail } from "@/api";

const state = {
    detailInfo:{}
};
const mutations = {
    SET_DETAILINFO(state,detailInfo){
        state.detailInfo = detailInfo
    }
};
const actions = {
    async getDetailInfo({commit},skuId){
        try {
            let result = await reqGoodsDetail(skuId) 
            console.log(result);
            if(result && result.code == 200){
                commit('SET_DETAILINFO',result.data)
            }
        } catch (error) {
            alert('获取商品详情失败')
            return Promise.reject(error)
        }
    }
};
const getters = {
    // 面包屑
    categoryView(state){
        return state.detailInfo.categoryView || {};
    },
    // 商品详情
    skuInfo(state){
        return state.detailInfo.skuInfo || {};
    },
    // 轮播图
    skuInfoList(state){
        if(state.detailInfo.skuInfo && state.detailInfo.skuInfo.skuImageList){
            return state.detailInfo.skuInfo.skuImageList
        } else {
            return []
        }
    },
    // 颜色
    spuSaleAttrList(state){
        return state.detailInfo.spuSaleAttrList || [];
    }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
